package com.yunji.product.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Optional;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.model.cache.ProductAttrCacheDTO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.bo.ProductAttrsListQueryBO;
import com.yunji.product.dao.IProductAttrsDao;
import com.yunji.product.domain.ProductAttrDO;
import com.yunji.product.mapper.ProductAttrsMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class ProductAttrsDaoImpl extends BaseDaoImpl<ProductAttrsMapper, ProductAttrDO> implements IProductAttrsDao {
    @Override
    public List<ProductAttrDO> search(ProductAttrsListQueryBO bo) {
        return list(new LambdaQueryWrapper<ProductAttrDO>()
                .eq(BeanUtils.isNotNull(bo.getAttrId()), ProductAttrDO::getAttrId, bo.getAttrId())
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductAttrDO::getProductId, bo.getProductId()));
    }

    @Override
    public int statsOfProductAttr(Long... ids) {
        Long count = lambdaQuery().in(ProductAttrDO::getAttrId, Arrays.asList(ids)).count();
        return Optional.fromNullable(count).or(0L).intValue();
    }

    @Override
    public boolean deleteByProductIds(Long... ids) {
        return remove(new LambdaQueryWrapper<ProductAttrDO>()
                .in(ProductAttrDO::getProductId, Arrays.asList(ids)));
    }

    @Override
    public List<ProductAttrDO> listByProductId(Long productId) {
        return lambdaQuery().eq(ProductAttrDO::getProductId, productId).list();
    }
}
